<template>
  <div class="create-or-edit">
    <el-form :model="role">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="role.name"></el-input>
      </el-form-item>
      <el-form-item label="角色编码" prop="code">
        <el-input v-model="role.code"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input type="textarea" v-model="role.description"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="onCancel">取消</el-button>
        <el-button type="primary" @click="onSubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createOrUpdate } from '@/services/role'
export default {
  name: 'CreateOrEdit',
  props: {
    role: {
      type: Object
    }
  },
  data () {
    return {
      roleModel: {}
    }
  },
  watch: {
    role: {
      deep: true,
      handler (o) {
        this.roleModel = JSON.parse(JSON.stringify(o))
      }
    }
  },
  // created () {
  //   if (this.isEdit) {
  //     this.loadRole()
  //   }
  // },
  methods: {
    // async loadRole () {
    //   const { data } = await getRoleById(this.roleId)
    //   if (data.code === '000000') {
    //     this.role = data.data
    //   }
    // },
    async onSubmit () {
      const { data } = await createOrUpdate(this.role)
      if (data.code === '000000') {
        this.$emit('success')
        this.$message.success('添加成功')
        this.role = {}
      }
    },
    onCancel () {
      this.$emit('cancel')
      this.role = {}
    }
  }
}
</script>

<style>

</style>
